package cn.edu.cxcyzx.service.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.poi.ss.formula.functions.Roman;
import org.springframework.stereotype.Service;

import cn.edu.cxcyzx.entity.Check_JSON;
import cn.edu.cxcyzx.entity.Pros;
import cn.edu.cxcyzx.entity.Room_use;
import cn.edu.cxcyzx.entity.Room_usePOJO;
import cn.edu.cxcyzx.service.Room_useService;
import cn.edu.cxcyzx.supervisor.DaoFit;
import cn.edu.cxcyzx.supervisor.ResultDo;
import cn.edu.cxcyzx.util.Room_useInfo;


@Service("Room_useService")
public class Room_useServiceImpl implements Room_useService {
	
	@Resource
	public DaoFit daoFit;
	
	@Override
	public void insert_record(Check_JSON check_JSON) {
		// TODO Auto-generated method stub
		if(check_JSON!=null){
			List<Room_use> list=new ArrayList<>();
			List<Pros> pros=check_JSON.getPros();
			for (Pros pros2 : pros) {
				int[] array=pros2.getArray();
				for (int i = 0; i < array.length; i++) {
					list.add(new Room_use(
							pros2.getRoomId(), pros2.getProjectId(), 
								check_JSON.getDate(), i+1,array[i]));
				}
			}
			System.out.println(Arrays.toString(list.toArray()));
			daoFit.getRoom_useDao().insert_record(list);
		}
	}

	@Override
	public ResultDo<Map<Integer,Room_usePOJO[]>> getRoom_uses(Date date) {
		// TODO Auto-generated method stub
		ResultDo<Map<Integer,Room_usePOJO[]>> resultDo=new ResultDo<>();
		List<Room_usePOJO> list=daoFit.getRoom_useDao().getRoom_uses(date);
		if(list!=null&&list.size()>0){
			HashMap<Integer,Room_usePOJO[]> map=new HashMap<>();
			
			Iterator<Room_usePOJO> iterator=list.iterator();
			while (iterator.hasNext()) {
				Room_usePOJO pojo=iterator.next();
				if(map.isEmpty()||!map.containsKey(pojo.getProjectId())){
					Room_usePOJO[] pojos=new Room_usePOJO[3];
					pojos[0]=pojo;
					map.put(pojo.getProjectId(),pojos);
				}else{
					Room_usePOJO[] pojos=Arrays.copyOf(map.get(pojo.getProjectId()),3);
					map.remove(pojo.getProjectId());
					if(pojo.getCheck_time()==2){
						pojos[1]=pojo;
					}else{
						pojos[2]=pojo;
					}
					map.put(pojo.getProjectId(), pojos);
				}
			}
			Set<Entry<Integer,Room_usePOJO[]>> sets=map.entrySet();
			Iterator<Entry<Integer,Room_usePOJO[]>> iterator2=sets.iterator();
			while (iterator2.hasNext()) {
				Map.Entry<java.lang.Integer, cn.edu.cxcyzx.entity.Room_usePOJO[]> entry = (Map.Entry<java.lang.Integer, cn.edu.cxcyzx.entity.Room_usePOJO[]>) iterator2
						.next();
				System.out.println("--"+entry.getKey()+":"+entry.getValue()[0].toString()+","+entry.getValue()[1].toString()+","+entry.getValue()[2].toString());
				
			}
			resultDo.setResult(map);
			resultDo.setSuccess(true);
		}else{
			resultDo.setSuccess(false);
		}
		return resultDo;
	}

	@Override
	public Date getLastDate() {
		// TODO Auto-generated method stub
		return daoFit.getRoom_useDao().getLastDate();
	}

	@Override
	public Date getFirstDate() {
		// TODO Auto-generated method stub
		return daoFit.getRoom_useDao().getFirstDate();
	}

	@Override
	public Date getMaxDate(int projectId) {
		// TODO Auto-generated method stub
		return daoFit.getRoom_useDao().getMaxDate(projectId);
	}

	@Override
	public ResultDo<int[]> getNum_proANDdate(int projectId, Date date) {
		// TODO Auto-generated method stub
		ResultDo<int[]> resultDo=new ResultDo<>();
		Integer[] nums=daoFit.getRoom_useDao().getNumBy_proAndDay(projectId, date);
		if(nums!=null&&nums.length>0){
			resultDo.setSuccess(true);
			Arrays.toString(nums);
			int[] nums_2=new int[3];
			for (int i = 0; i < nums.length; i++) {
				nums_2[i]=nums[i];
			}
			resultDo.setResult(nums_2);
		}else{
			resultDo.setSuccess(false);
			resultDo.setMessage("信息未录入。");
		}
		return resultDo;
	}

	@Override
	public ResultDo<List<Room_useInfo>> getRoom_useInfo(int projectId, int range) {
		// TODO Auto-generated method stub
		ResultDo<List<Room_useInfo>> resultDo=new ResultDo<>();
		List<Room_use> list=daoFit.getRoom_useDao().getAllInfo(projectId);
		if(list!=null&&list.size()>0){
			HashMap<Date, Room_useInfo> map=new HashMap<>(list.size());
			Iterator<Room_use> it=list.iterator();
			while (it.hasNext()) {
				Room_use room_use = (Room_use) it.next();
				
				if(map.isEmpty()||!map.containsKey(room_use.getCheck_date())){
					Date date=room_use.getCheck_date();
					DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
					map.put(room_use.getCheck_date(),new Room_useInfo(projectId, format.format(date),room_use.getMember_num()));
					
				}else{
					map.get(room_use.getCheck_date()).add_count(room_use.getMember_num());
				}
				System.out.println("map:"+map.get(room_use.getCheck_date()).toString());
			}
			List<Room_useInfo> list2=new ArrayList<>(map.values());
			System.out.println("list排序前:"+Arrays.toString(list2.toArray()));
			Collections.sort(list2);
			System.out.println("list排序后:"+Arrays.toString(list2.toArray()));
			resultDo.setResult(list2);
			resultDo.setSuccess(true);
			
		}else{
			resultDo.setSuccess(false);
		}
		return resultDo;
	}

}
